package cn.sumpu.app.guoguojie.web.mvc.family;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import cn.sumpu.app.guoguojie.biz.AppEventBiz;
import cn.sumpu.app.guoguojie.biz.DoingBiz;
import cn.sumpu.app.guoguojie.biz.FamilyBiz;
import cn.sumpu.app.guoguojie.biz.FamilyTagBiz;
import cn.sumpu.app.guoguojie.biz.FriendBiz;
import cn.sumpu.app.guoguojie.biz.MemberBiz;
import cn.sumpu.app.guoguojie.biz.MessageBiz;
import cn.sumpu.app.guoguojie.biz.TagBiz;
import cn.sumpu.app.guoguojie.biz.VisitorBiz;
import cn.sumpu.app.guoguojie.constant.FamilyRole;
import cn.sumpu.app.guoguojie.domain.AppEvent;
import cn.sumpu.app.guoguojie.domain.Doing;
import cn.sumpu.app.guoguojie.domain.Family;
import cn.sumpu.app.guoguojie.domain.FamilyTag;
import cn.sumpu.app.guoguojie.domain.Member;
import cn.sumpu.app.guoguojie.domain.Tag;
import cn.sumpu.app.guoguojie.domain.Visitor;
import cn.sumpu.app.guoguojie.domain.Ext.FriendFamily;
import cn.sumpu.app.guoguojie.web.dto.Baby;
import cn.sumpu.app.guoguojie.web.dto.FamilyAll;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.Common;
import cn.sumpu.app.guoguojie.util.Encode;

@Controller
public class FamilyController {

	@Autowired
	private FamilyBiz familyBiz;
	@Autowired
	private MemberBiz memberBiz;
	@Autowired
	private VisitorBiz visitorBiz;
	@Autowired
	private FriendBiz friendBiz;
	@Autowired
	private DoingBiz doingBiz;
	@Autowired
	private MessageBiz messageBiz;
	@Autowired
	private TagBiz tagBiz;
	@Autowired
	private FamilyTagBiz familyTagBiz;
	@Autowired
	private AppEventBiz eventBiz;

	// 注册页面
	@RequestMapping(value = "/enterRegister.ajax", method = RequestMethod.GET)
	public String enterRegister(HttpServletRequest request) {
		return "family/register";
	}

	// 邮箱验证
	@RequestMapping(value = "/checkEmail.ajax", method = RequestMethod.GET)
	public void checkEmail(HttpServletRequest request,
			HttpServletResponse response) throws AppBusinessException {
		String email = request.getParameter("email");
		if (Common.empty(email))
			return;
		response.setContentType("text/plain; charset=UTF-8");
		PrintWriter pw = null;
		try {
			pw = response.getWriter();
			Member m = memberBiz.getMemberEmail(email);
			if (m != null) {
				pw.print(false);
			} else {
				pw.print(true);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 密码验证
	@RequestMapping(value = "/checkPassword.ajax", method = RequestMethod.GET)
	public void checkPassword(HttpServletRequest request,
			HttpServletResponse response) throws AppBusinessException {
		String password = request.getParameter("password");
		Member member = (Member) request.getSession().getAttribute("member");
		if (Common.empty(password))
			return;
		response.setContentType("text/plain; charset=UTF-8");
		PrintWriter pw = null;
		try {
			pw = response.getWriter();
			List<Member> memberList = memberBiz.getMemberListByDoorplate(member
					.getFamily());
			boolean pswNotExist = true;
			for (Member me : memberList) {
				if (Common.md5(Common.md5(password)).equals(me.getPassword())) {
					pswNotExist = false;
				}
			}
			pw.print(pswNotExist);

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 创建家庭
	@RequestMapping(value = "/register.ajax", method = RequestMethod.POST)
	public String register(HttpServletRequest request,
			HttpServletResponse response) {
		FamilyAll familyAll = null;
		response.setContentType("text/plain; charset=UTF-8");
		PrintWriter pw = null;

		try {
			pw = response.getWriter();
			String escaped_json = request.getParameter("jsonData");
			String json = Encode.unescape(escaped_json);
			JSONObject jsonObject = JSONObject.fromObject(json);
			Map<String, Class<?>> m = new HashMap<String, Class<?>>();
			m.put("babyList", Baby.class);
			familyAll = (FamilyAll) JSONObject.toBean(jsonObject,
					FamilyAll.class, m);
			Family f = new Family();
			f.setNickname(familyAll.getNickname());
			f.setProvice(familyAll.getProvince());
			f.setCity(familyAll.getCity());
			f.setDistrict(familyAll.getDistrict());
			Member member = new Member();
			member.setEmail(familyAll.getEmail());
			member.setUsername(familyAll.getUsername());
			member.setRole(familyAll.getRole());
			member.setPassword(familyAll.getPassword());

			List<Member> memberList = new ArrayList<Member>();
			List<Baby> babyList = familyAll.getBabyList();
			if (babyList != null && babyList.size() > 0) {
				for (Baby baby : babyList) {
					Member mBaby = new Member();
					mBaby.setUsername(baby.getBabyName());
					mBaby.setSex(baby.getBabySex());
					mBaby.setBirthday(baby.getBirthday());
					memberList.add(mBaby);
				}
			}
			String path = request.getContextPath();
			String basePath = request.getScheme() + "://"
					+ request.getServerName() + ":" + request.getServerPort()
					+ path + "/";
			Member memeberSession = familyBiz.register(basePath, f, member,
					memberList);
			request.getSession(true).setAttribute("member", memeberSession);
			pw.print("success");
		} catch (Exception e) {
			e.printStackTrace();
			pw.print("error");
		}
		return null;
	}

	// 激活会员账号
	@RequestMapping(value = "/activeMember.ajax", method = RequestMethod.GET)
	public String activeMember(HttpServletRequest request) {
		try {
			String mId = request.getParameter("id");
			String aCode = request.getParameter("v");
			if (familyBiz.activeMember(mId, aCode)) {
				Member member = memberBiz.getMemberById(mId);
				List<Member> memberList = memberBiz
						.getMemberListByDoorplate(member.getFamily());
				Family family = familyBiz.getFamilyByDoor(member.getFamily());
				List<FriendFamily> friendList = friendBiz
						.getMyFriendsFamilySize(member.getFamily(), 6);
				List<Visitor> visitorList = visitorBiz.visitorMemberSize(
						member.getFamily(), 6);
				request.setAttribute("friendList", friendList);
				request.setAttribute("visitorList", visitorList);
				request.setAttribute("memberList", memberList);
				request.setAttribute("family", family);
				return "family/myFamily";
			}
			request.setAttribute("errorMsg", "Can't active member account!");
			return "common/error";
		} catch (Exception e) {
			request.setAttribute("errorMsg", e.getLocalizedMessage());
			return "common/error";
		}
	}

	// 注册成功页面
	@RequestMapping(value = "/registerResult.ajax", method = RequestMethod.GET)
	public String registerResult() {
		return "family/registerResult";
	}

	@RequestMapping(value = "/enterLogin.ajax", method = RequestMethod.GET)
	public String enterLogin() {
		return "family/login";
	}

	@RequestMapping(value = "/enterResult.htm", method = RequestMethod.GET)
	public String enterResult(HttpServletRequest request) {
		request.setAttribute("doorplate", request.getParameter("doorplate"));
		return "family/registerResult";
	}

	@RequestMapping(value = "/logout.htm", method = RequestMethod.GET)
	public String logout(HttpServletRequest request) {
		request.getSession().invalidate();
		return "family/login";
	}

	// 登录
	@RequestMapping(value = "/login.ajax", method = RequestMethod.POST)
	public String login(String loginName, String loginPwd,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		List<Member> memberList = null; 
		try {   
			memberList = memberBiz.login(loginName, loginPwd);
		} catch (AppBusinessException e) { 
			request.setAttribute("errorMsg", e.getLocalizedMessage());
			return "common/error";
		}
		if (memberList != null && memberList.size()>0) {
			request.getSession(true).setAttribute("loginMemberList", memberList);
			request.getSession(true).setAttribute("member", memberList.get(0));
			response.sendRedirect("family?id=" + memberList.get(0).getFamily());
			return null;
		} else {
			request.setAttribute("loginError", "登陆失败，用户名或密码输入错误或用户名不存在请马上注册");
			return "family/login";
		}
	}

	// 家页面
	@RequestMapping(value = "/family")
	public String family(HttpServletRequest request) {
		try {
			// 传过来家的门牌号
			String familyId = request.getParameter("id");
			HttpSession session = request.getSession();
			Member member = null;
			String myFamilyId = null;
			if (null != session) {
				member = (Member) request.getSession().getAttribute("member");
				if (null != member) {
					myFamilyId = member.getFamily();
				}
			}
			Family family = familyBiz.getFamilyByDoor(familyId);
			List<Member> memberList = memberBiz
					.getMemberListByDoorplate(familyId);
			request.setAttribute("family", family);
			request.setAttribute("memberList", memberList);
			// 判断是否是自己家
			if (null != myFamilyId && null != familyId
					&& myFamilyId.equals(familyId)) {
				int count = messageBiz.messageAllNoReadCount(myFamilyId);
				int doingCount = doingBiz.getDoingListCount(familyId);
				request.setAttribute("doingCount", doingCount);
				request.setAttribute("messageNoRead",
						Common.getCountFormat(count));
				return "family/myFamily";
			} else {
				if (member != null && !Common.empty(member.getIdentifier())) {
					Visitor visitor = visitorBiz.visitOtherFamily(familyId,
							member.getIdentifier());
					Doing doing = doingBiz.getDoingByFamily(familyId);
					// 查询谁发的微博
					if (doing != null) {
						Member memberDoing = memberBiz.getMemberById(doing
								.getMember());
						request.setAttribute("memberDoing", memberDoing);
					}
					request.setAttribute("visitor", visitor);
					request.setAttribute("doing", doing);
					// 查看是否是好友
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("family", member.getFamily());
					map.put("friend", familyId);
					request.setAttribute("isFriend", "NO");
					if (friendBiz.getFriendByFamily(map) > 0)
						request.setAttribute("isFriend", "YES");
					Family myFamily = familyBiz
							.getFamilyByDoor(((Member) request.getSession()
									.getAttribute("member")).getFamily());
					request.setAttribute("myFamily", myFamily);
					request.setAttribute("otherFamilyId", familyId);
					request.setAttribute("albumId",
							request.getParameter("albumId"));
					request.setAttribute("blogId",
							request.getParameter("blogId"));
				}
				return "family/taFamily";
			}
		} catch (Exception e) {
			request.setAttribute("errorMsg", "Can't enter family!");
			request.setAttribute("exception", e);
			return "common/error";
		}
	}

	// 添加成员页面
	@RequestMapping(value = "/toAddMember.htm", method = RequestMethod.GET)
	public String toAddMember(HttpServletRequest request) {
		String doorplate = request.getParameter("doorplate");
		Member member = (Member) request.getSession().getAttribute("member");
		try {
			List<Member> memberList = memberBiz.getMemberListByDoorplate(member
					.getFamily());
			List<FamilyRole> roleList = new ArrayList<FamilyRole>();
			roleList.add(FamilyRole.FATHER);
			roleList.add(FamilyRole.MOTHER);
			for (Member me : memberList) {
				if (FamilyRole.FATHER.getValue().equals(me.getRole())) {
					roleList.remove(FamilyRole.FATHER);
				} else if (FamilyRole.MOTHER.getValue().equals(me.getRole())) {
					roleList.remove(FamilyRole.MOTHER);
				}

			}
			request.setAttribute("roleList", roleList);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		request.setAttribute("doorplate", doorplate);
		return "family/addMember";
	}

	// 添加成员处理
	@RequestMapping(value = "/addMember.htm", method = RequestMethod.POST)
	public String addMember(@ModelAttribute("member") Member member,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		try {
			Member userMember = (Member) request.getSession().getAttribute(
					"member");
			member.setFamily(userMember.getFamily());
			// 添加家庭成员
			String path = request.getContextPath();
			String basePath = request.getScheme() + "://"
					+ request.getServerName() + ":"
					+ request.getServerPort() + path + "/";
			memberBiz.addMember(basePath, member);
			member = memberBiz.getHostInfoByDoorplate(member.getFamily());
			request.getSession(true).setAttribute("member", member);
			response.sendRedirect("family?id="+member.getFamily());
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return "common/error";
		}
		
	}

	// 添加成员验证处理
	@RequestMapping(value = "/checkMember.htm", method = RequestMethod.POST)
	public String checkMember(@ModelAttribute("member") Member member,
			String doorplate, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter output = response.getWriter();

		try {
			Member m = memberBiz.getMemberPwdDoorplateEmail(
					member.getPassword(), doorplate, member.getEmail());
			if (m != null) {
				output.write("error");
				output.flush();
			} else {
				output.write("success");
			}
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return null;
	}

	// 邮箱验证
	@RequestMapping(value = "/checkEmail.htm", method = RequestMethod.POST)
	public String checkEmail(@ModelAttribute("member") Member member,
			String doorplate, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter output = response.getWriter();
		try {
			Member m = memberBiz.getMemberEmail(member.getEmail());
			if (m != null) {
				output.write("error");
				output.flush();
			} else {
				output.write("success");
			}
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return null;
	}

	// 获取验证码图
	@RequestMapping(value = "/img.ajax", method = RequestMethod.GET)
	public String registerIg(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		// 在内存中绘图:
		String code = String.valueOf((int) (Math.random() * 9000) + 1000);
		BufferedImage image = new BufferedImage(100, 50,
				BufferedImage.TYPE_INT_RGB);
		Graphics2D g = image.createGraphics();
		g.setColor(Color.GRAY);
		g.fillRect(0, 0, 100, 50);
		g.setColor(Color.RED);
		g.drawRect(0, 0, 99, 49);
		g.setColor(Color.BLACK);
		g.drawString(code, 20, 20);
		g.dispose();
		image.flush();
		// 设置ContentType:
		response.setContentType("image/jpeg");
		response.setHeader("Cache-Control", "no-cache");
		// 输出到ServletOutputStream:
		ServletOutputStream output = response.getOutputStream();
		// 储存验证码在session
		request.getSession().setAttribute("code", code);

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ImageIO.write(image, "jpeg", baos);
		output.write(baos.toByteArray());
		output.flush();
		return null;
	}

	// 刷新父母的头像
	@RequestMapping(value = "/parentsAvatar.htm")
	public String refrestFamilyAvatar(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			Member member = (Member) request.getSession()
					.getAttribute("member");
			List<Member> memberList = memberBiz.getMemberListByDoorplate(member
					.getFamily());
			request.setAttribute("memberList", memberList);
			return "member/parentsAvatar";
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return null;
	}

	// 刷新孩子的头像
	@RequestMapping(value = "/babysAvatar.htm")
	public String refreshBabysAvatar(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			Member member = (Member) request.getSession()
					.getAttribute("member");
			List<Member> memberList = memberBiz.getMemberListByDoorplate(member
					.getFamily());
			request.setAttribute("memberList", memberList);
			return "member/babysAvatar";
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return null;
	}

	// 我家的主页的右边栏
	@RequestMapping(value = "/myFamilyRight.htm")
	public String myFamilyRight(HttpServletRequest request,
			HttpServletResponse response) {
		Member member = (Member) request.getSession().getAttribute("member");
		try {
			List<FriendFamily> friendList = friendBiz.getMyFriendsFamilySize(
					member.getFamily(), Common.PAGE_SIZE_MY_FAMILY_FRIEND);
			List<Visitor> visitorList = visitorBiz.visitorMemberSize(
					member.getFamily(), Common.PAGE_SIZE_MY_FAMILY_VISITOR);
			List<FriendFamily> maybeFriends = friendBiz.maybeFriendsMain(member
					.getFamily());
			// 日历事件条数
			int mainEventCount = eventBiz.getMainAppEventCount(member
					.getFamily());
			request.setAttribute("mainEventCount",
					Common.getCountFormat(mainEventCount));
			// 今天日期和星期几
			Date date = new Date();
			request.setAttribute(
					"todayFormat",
					Common.getFormatDate(date, "yyyy年MM月dd日") + " "
							+ Common.getWeekDay(date));
			// 今天最近的一条
			AppEvent lastEvent = eventBiz.getAppEventLatestNow(member
					.getFamily());
			request.setAttribute("lastEvent", lastEvent);
			// 未来的最近的一条
			AppEvent futureEvent = eventBiz.getAppEventLatestFutre(member
					.getFamily());
			request.setAttribute("futureEvent", futureEvent);
			// 我家的标签
			List<FamilyTag> familyTagList = familyTagBiz
					.getFamilyTagByDoorplate(member.getFamily());
			request.setAttribute("familyTagList", familyTagList);
			request.setAttribute("maybeFriends", maybeFriends);
			request.setAttribute("friendList", friendList);
			request.setAttribute("visitorList", visitorList);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "family/myFamilyRight";
	}

	// 他家的主页的右边栏
	@RequestMapping(value = "/taFamilyRight.htm")
	public String taFamilyRight(HttpServletRequest request,
			HttpServletResponse response, String family) {
		try {
			List<FriendFamily> friendList = friendBiz.getMyFriendsFamilySize(
					family, Common.PAGE_SIZE_MY_FAMILY_FRIEND);
			List<Visitor> visitorList = visitorBiz.visitorMemberSize(family,
					Common.PAGE_SIZE_MY_FAMILY_VISITOR);
			Family taFamily = familyBiz.getFamilyByDoor(family);
			// List<FriendFamily> maybeFriends =
			// friendBiz.maybeFriendsMain(family);
			// request.setAttribute("maybeFriends", maybeFriends);
			List<FamilyTag> familyTagList = familyTagBiz
					.getFamilyTagByDoorplate(family);
			request.setAttribute("familyTagList", familyTagList);
			request.setAttribute("taFamily", taFamily);
			request.setAttribute("friendList", friendList);
			request.setAttribute("visitorList", visitorList);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "family/taFamilyRight";
	}

	// family的弹出框
	@RequestMapping(value = "/familyTip.htm")
	public String familyTip(HttpServletRequest request,
			HttpServletResponse response, String family) {
		try {
			Family f = familyBiz.getFamilyByDoor(family);
			request.setAttribute("family", f);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "family/familyTip";
	}

	// myFamily的内容
	@RequestMapping(value = "/myFamilyMain.htm")
	public String myFamilyMain(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			Member member = (Member) request.getSession()
					.getAttribute("member");
			Family family = familyBiz.getFamilyByDoor(member.getFamily());
			request.setAttribute("isActive",
					memberBiz.getMemberById(member.getIdentifier())
							.getIsActive());
			request.setAttribute("family", family);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "family/myFamilyMain";
	}

	// 进入家的标签页面
	@RequestMapping(value = "/familyTag.htm")
	public String familyTag(Integer page, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			Member member = (Member) request.getSession()
					.getAttribute("member");
			if (page == null)
				page = 1;
			List<Tag> tagList = tagBiz.getTagByDoorplate(member.getFamily(),
					page);
			Integer tagCount = tagBiz
					.getTagByDoorplateCount(member.getFamily());
			List<FamilyTag> familyTagList = familyTagBiz
					.getFamilyTagByDoorplate(member.getFamily());
			request.setAttribute("page", page);
			request.setAttribute("tagCount", tagCount / 20 == 0 ? tagCount / 20
					: tagCount / 20 + 1);
			request.setAttribute("tagList", tagList);
			request.setAttribute("familyTagList", familyTagList);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "family/familyTag";
	}

	// 换一批标签
	@RequestMapping(value = "/nextFamilyTag.htm")
	public String nextFamilyTag(Integer page, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			Member member = (Member) request.getSession()
					.getAttribute("member");
			if (page == null)
				page = 1;
			List<Tag> tagList = tagBiz.getTagByDoorplate(member.getFamily(),
					page);
			request.setAttribute("tagList", tagList);
			request.setAttribute("page", page);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "family/nextFamilyTag";
	}

	// 保存家的标签
	@RequestMapping(value = "/saveFamilyTag.htm", method = RequestMethod.POST)
	public String saveFamilyTag(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			response.setContentType("text/html; charset=UTF-8");
			Member member = (Member) request.getSession()
					.getAttribute("member");
			String tag = request.getParameter("tagList");
			String tagArr[] = null;
			if (tag != null)
				tagArr = tag.split("_");
			familyTagBiz.insertFamilyTagList(tagArr, member.getFamily());
			response.getWriter().print("success");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 
	 * @param number	会员ID
	 * @param request
	 * @param response
	 * @return 进入邮箱验证后的成功页面
	 * @throws AppBusinessException
	 */
	@RequestMapping(value = "/successValede.ajax", method = RequestMethod.GET)
	public String successValede(String number, HttpServletRequest request,
			HttpServletResponse response) throws AppBusinessException {
		try {
			Member member = memberBiz.getMemberById(number);
			if (member == null) {
				return "common/error";
			} else {
				member.setIsActive(true);
				memberBiz.updateMember(member);
				request.getSession(true).setAttribute("member", member);
				return "member/emailValide";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "common/error";
		}
	}

	/**
	 * 再次发送邮箱验证的邮件 author Bruce Zhang
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws AppBusinessException
	 */
	@RequestMapping(value = "/sendEmailAgain.htm", method = RequestMethod.GET)
	public String sendEmailAgain(HttpServletRequest request,
			HttpServletResponse response) throws AppBusinessException {
		try {
			response.setContentType("text/plain; charset=UTF-8");
			Member member = (Member) request.getSession()
					.getAttribute("member");
			String path = request.getContextPath();
			String basePath = request.getScheme() + "://"
					+ request.getServerName() + ":" + request.getServerPort()
					+ path + "/";
			memberBiz.sendEmailToMember(basePath, member);
			response.getWriter().print("success");
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return "common/error";
		}
	}

	// 保存家的标签
	@RequestMapping(value = "/saveSkin.htm", method = RequestMethod.POST)
	public String saveSkin(String skin, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			response.setContentType("text/html; charset=UTF-8");
			Member member = (Member) request.getSession()
					.getAttribute("member");
			Family family = member.getFamilyObj();
			family.setSkin(skin);
			familyBiz.updateFamily(family);
			response.getWriter().print("success");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	// 添加成员处理
	@RequestMapping(value = "/saveFamilyInfo.htm", method = RequestMethod.POST)
	public String saveFamilyInfo(String nickname, String introduction,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		try {
			PrintWriter out = response.getWriter();
			Member member = (Member) request.getSession().getAttribute("member");
			// 取得该会员的家庭信息
			Family family = member.getFamilyObj();
			family.setNickname(nickname);
			family.setIntroduction(introduction);
			// 更新家庭信息
			familyBiz.updateFamily(family);
			response.setContentType("text/html; charset=UTF-8");
			out.print("success");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
